package com.browsesoft.user;

import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.browsesoft.EntityManager;
import com.browsesoft.ExtendElement;
import com.browsesoft.ManagerGroup;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLTableModel;

public class PostTableModel implements HTMLTableModel {
	/**
	 * 基本组件
	 */
	private HTMLBasicComponent component;

	private LinkedList users;

	private Role role;

	/**
	 * 初始化model
	 */
	public void init(HttpServletRequest request) throws Exception {
	}

	/**
	 * 得到列数
	 * 
	 * @return 列数
	 */
	public int getColumnCount() {
		return 3;
	}

	/**
	 * 得到列的名称
	 * 
	 * @param index
	 *            列号
	 * @return 列的名称
	 */
	public String getColumnName(int index) {
		String[] tableHead = { "用户名称", "修改", "删除" };
		return tableHead[index];
	}

	/**
	 * 得到列的英文名
	 * 
	 * @param index
	 *            列号
	 * @return 列的英文名
	 */
	public String getColumnEname(int index) {
		return "";
	}

	public Object getDatas() {
		return null;
	}

	/**
	 * 得到行数
	 * 
	 * @return 行数
	 */
	public int getRowCount() {
		return users.size();
	}

	/**
	 * 设置传递的参数
	 * 
	 * @param request
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 得到传递参数
		String id = request.getParameter("point");
		EntityManager entitymanager = (EntityManager) ManagerGroup
				.getManagerGroup().getManager("entity");
		if (id != null) {
			role = (Role) entitymanager.getObject(id);
			users = role.getUsers();
		}
	}

	/**
	 * 得到某列的操作类型
	 * 
	 * @return 类型
	 */
	public String getType(int col) {
		String[] types = { "td", "modify", "delete" };
		return types[col];
	}

	/**
	 * 修改单元值
	 */
	public void modifyCellValue(Element td, Document doc, int row, int col) {
		try {
			Element element = (Element) this.component.getPage().getDocument()
					.getElementsByTagName("information").item(0);
			ExtendElement.replaceElementContext(element, doc
					.createTextNode((String) role.getAttributes().get("name")));
			EntityManager entitymanager = (EntityManager) ManagerGroup
					.getManagerGroup().getManager("entity");
			// 根据行数取得资源
			User resource = (User) entitymanager.getObject((String) users
					.get(row));
			if (col == 0) {
				// 如果列数等于0替换了列元素
				ExtendElement.replaceElementContext(td, doc
						.createTextNode((String) resource.getAttributes().get(
								"name")));
			} else if (col == 1) {
				Element e = (Element) ExtendElement.findElements(td, "type",
						"link").get(0);
				e.setAttribute("onclick", "");
			} else if (col == 2) {
				Element e = (Element) ExtendElement.findElements(td, "type",
						"link").get(0);
				// str = str.replaceAll("id=", "id=" + resource.getID());
				e.setAttribute("onclick", "");
			}
		} catch (Exception eee) {
			eee.printStackTrace();
		}
	}

	/**
	 * 设置模型所对应的组件
	 * 
	 * @param component
	 *            对应的组件
	 */
	public void setComponent(HTMLBasicComponent component) {
		this.component = component;
	}

	/**
	 * 模型更新页面元素
	 * 
	 * @throws Exception
	 */
	public void updatePageElement(HTMLBasicComponent c) throws Exception {
	}

	/**
	 * 设置条件
	 */
	public void setCondition(String condition) {
	}
}